<template>
  <div class="sales-record-container">
    <!-- 查询 -->
    <div class="search-container">
      <el-date-picker
          v-model="queryDateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="margin-right: 10px;"
          @change="queryByDateRange"
      ></el-date-picker>
      <el-button
          @click="queryByDateRange"
          type="primary"
          plain
          class="search-button"
      >
        查询
      </el-button>
    </div>

    <!-- 错误信息 -->
    <div class="error-message">{{ errorMessage }}</div>

    <!-- 销售记录表格 -->
    <el-table
        :data="salesRecordList"
        class="sales-record-table"
        header-cell-class-name="table-header"
    >
      <el-table-column
          label="序号"
          type="index"
          width="60px"
          align="center"
      ></el-table-column>
      <el-table-column
          prop="id"
          label="记录 ID"
          width="100px"
          align="center"
      ></el-table-column>
      <el-table-column
          prop="orderId"
          label="订单 ID"
          width="100px"
          align="center"
      ></el-table-column>
      <el-table-column
          prop="productId"
          label="产品 ID"
          width="100px"
          align="center"
      ></el-table-column>
      <el-table-column
          prop="productName"
          label="产品名称"
          width="150px"
          align="center"
      ></el-table-column>
      <el-table-column
          prop="quantity"
          label="销售数量"
          width="120px"
          align="center"
      ></el-table-column>
      <el-table-column
          prop="amount"
          label="销售金额"
          width="120px"
          align="center"
      ></el-table-column>
      <el-table-column
          prop="salesDate"
          label="销售日期"
          width="150px"
          align="center"
      ></el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
          background
          v-model:current-page="pageContains.pageNum"
          v-model:page-size="pageContains.pageSize"
          :page-sizes="[10, 20, 30, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageContains.total"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
          class="pagination"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import http from '@/axios/http';

// 销售记录数据集合
let salesRecordList = ref([]);

// 报错提示
let errorMessage = ref("");

// 查询日期范围
let queryDateRange = ref(null);

// 分页相关
let pageContains = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
});

// 根据日期范围查询销售记录
function queryByDateRange() {
  let param = {
    startDate: queryDateRange.value ? queryDateRange.value[0] : null,
    endDate: queryDateRange.value ? queryDateRange.value[1] : null,
    pageNum: pageContains.value.pageNum,
    pageSize: pageContains.value.pageSize
  };
  http.post('/sales/query_by_date_range', param).then((res) => {
    let result = res.data;
    if (result.result === 'success') {
      pageContains.value = result.data;
      salesRecordList.value = result.data.list;
    } else {
      errorMessage.value = result.error;
    }
  });
}

// 获取所有销售记录
function getAllSalesRecords() {
  let params = {
    pageNum: pageContains.value.pageNum,
    pageSize: pageContains.value.pageSize
  };
  http.post('/sales/get_all_sales_records', params).then((res) => {
    let result = res.data;
    if (result.result === 'success') {
      pageContains.value = result.data;
      salesRecordList.value = result.data.list;
    } else {
      errorMessage.value = result.error;
    }
  });
}

// 页面初始化
onMounted(() => {
  getAllSalesRecords();
});

// 分页处理
const handlePageChange = (pageNum1) => {
  pageContains.value.pageNum = pageNum1;
  queryByDateRange();
};

const handleSizeChange = (pageSize1) => {
  pageContains.value.pageSize = pageSize1;
  queryByDateRange();
};
</script>

<style scoped>
.sales-record-container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.search-container {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.search-button {
  margin-left: 10px;
}

.error-message {
  color: #f56c6c;
  margin-bottom: 20px;
  text-align: center;
}

.sales-record-table {
  width: 100%;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.table-header {
  background-color: #f0f2f5;
  font-weight: bold;
  color: #333;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.pagination {
  width: 100%;
}
</style>